package com.tange365.demo.activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.tange365.demo.ipc.R;
import com.tange365.demo.ipc.BuildConfig;
import com.tg.app.activity.device.add.ApSetWifiActivityEx;
import com.tange.base.toolkit.StatusBarUtil;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.util.TGPushUtils;
import com.tg.login.activity.LoginActivity;

import java.util.Set;

public class LaunchActivity extends AppCompatActivity {

    private static final String TAG = "LaunchActivityTAG";

    protected void onCreate(Bundle savedInstanceState) {
        TGLog.i(TAG, "onCreate: ...");

        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        StatusBarUtil.setStatusBarTransparent(this);

        if (!this.isTaskRoot()) {
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                    return;
                }
            }
        }
        setContentView(R.layout.activity_launch);
    }

    @Override
    protected void onResume() {
        super.onResume();
        TGLog.i(TAG, "onResume: ...");
        Intent intent = new Intent(LaunchActivity.this, LoginActivity.class);
        intent.putExtra(ApSetWifiActivityEx.EXT_WIFI_UUID, "");
        intent.putExtra("start_type", "launch");

        // VIVO推送渠道需要把参数透传过去，否则无法响应消息推送的打开行为
        Intent originIntent = getIntent();
        if (TGPushUtils.isIntentContainsVivoMessage(originIntent)) {
            TGLog.i(TAG, "put extras for vivo push");
            intent.putExtras(originIntent.getExtras());
        }

        printIntentData();

        startActivity(intent);
        finish();
    }

    private void printIntentData() {
        TGLog.i(TAG, "printIntentData: ...");

        if (BuildConfig.DEBUG) {
            Intent originIntent = getIntent();
            if (originIntent == null) return;
            Bundle bundle = originIntent.getExtras();
            if (bundle == null) return;

            Set<String> keys = bundle.keySet();
            for (String key : keys) {
                TGLog.i(TAG, "printIntentData: key = " + key + " , value = " + bundle.get(key));
            }
        }
    }
}
